﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sern.Abstracts.Tracker.DataContracts;
using Sern.Common;
using Sern.Common.Fakes;
using Sern.ServiceImplementation.TrackerDefinition;
using Sern.ServiceImplementation.TrackerDefinition.Data;
using Microsoft.Practices.Unity;

namespace Sern.Test.ServiceImplementation.TrackerDefinition
{
    [TestClass]
// ReSharper disable InconsistentNaming
    public class TrackerCommon_SubscribeNewClient
// ReSharper restore InconsistentNaming
    {
        [TestInitialize]
        public void Initialize()
        {
            ComponentLocator.Reset();

            ComponentLocator.Unity.RegisterInstance(
                    RepositoryFactory.CreateStub<Client>());

            ComponentLocator.Unity.RegisterTestInstance(Guid.NewGuid(), "jobId");

            ComponentLocator.Unity.RegisterTestInstance(
                new ClientInfo
                    {
                        JobId = ComponentLocator.Unity.ResolveTest<Guid>("jobId"),
                        Id = Guid.NewGuid(),
                        KeyShape = "NewKey.Shape",
                        Type = ClientType.Tracker,
                        Uri = "asdf",
                        ValueShape = "SomeValue.Shape",
                        HostUri = "http://clientHost:8080"
                    },
                    "client1"
                );

            ComponentLocator.Unity.RegisterTestInstance(
                new ClientInfo
                {
                    JobId = ComponentLocator.Unity.ResolveTest<Guid>("jobId"),
                    Id = Guid.NewGuid(),
                    KeyShape = "AnotherKey.Shape",
                    Type = ClientType.Filter,
                    Uri = "asdf",
                    ValueShape = "SomeValue.Shape",
                    HostUri = ""
                },
                "client2"
            );

            
        }

        [TestMethod]
        public void Test()
        {
            var client1Info = ComponentLocator.Unity.ResolveTest<ClientInfo>("client1");
            TrackerCommon.Subscribe(
                client1Info.JobId,
                client1Info.Id,
                client1Info.Type,
                client1Info.Uri,
                client1Info.KeyShape,
                client1Info.ValueShape, 
                client1Info.HostUri
                );

            var client2Info = ComponentLocator.Unity.ResolveTest<ClientInfo>("client2");
            TrackerCommon.Subscribe(
                client2Info.JobId,
                client2Info.Id,
                client2Info.Type,
                client2Info.Uri,
                client2Info.KeyShape,
                client2Info.ValueShape,
                client2Info.HostUri
                );

            Assert.IsTrue(
                ComponentLocator.Unity
                    .Resolve<IRepository<Client>>().Records.Any(_ => _.Id == client1Info.Id));

            Assert.IsTrue(
                ComponentLocator.Unity
                    .Resolve<IRepository<Client>>().Records.Any(_ => _.Id == client2Info.Id));
        }
    }
}
